package org.nanotek.tika.job.reader;

import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

import org.nanotek.AbstractBase;
import org.nanotek.Base;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;


public class QueueItemReader implements ItemReader<Queue<Base<?>>> {

	Queue<Base<?>> fileNameQueue ; 
	private boolean done = false; 
	
    public static void main(String[] args)
        throws IOException {

//        if (args.length < 3 
//            || !args[1].equals("-name"))
//            usage();

    	QueueItemReader find = new QueueItemReader();
        try {
			Queue <Base<?>> baseQueue = find.read();
			Base<?> element = null;
			while ((element = baseQueue.poll())!=null)
				System.out.println(element.getId());
        } catch (Exception e) {
			e.printStackTrace();
		}
//        Finder finder = new Finder(pattern);
//        Files.walkFileTree(startingDir, finder);
//        finder.done();
    }

	@Override
	public Queue<Base<?>> read() throws Exception, UnexpectedInputException,
			ParseException, NonTransientResourceException {
		// TODO Auto-generated method stub
		if (done) return null;
		Queue<Base<?>> reference = new LinkedBlockingDeque<Base<?>>();
        Path startingDir = Paths.get("/home/java-eclipse/hk2~git/");
        String pattern = "**.java";
		Finder finder = new Finder(pattern , reference);
        Files.walkFileTree(startingDir, finder);
        finder.done();
        done = true;
		return reference;
	}
	
	public class Finder extends SimpleFileVisitor<Path> {

        private final PathMatcher matcher;
        private int numMatches = 0;
        private Path startingDir = null;
        private Queue<Base<?>> reference;
        
        Finder(String pattern) {
            matcher =
                FileSystems.getDefault()
                    .getPathMatcher("glob:" + pattern);
        }

        public Finder(String pattern, Queue<Base<?>> reference) {
        	matcher =
                    FileSystems.getDefault()
                        .getPathMatcher("glob:" + pattern);
        	this.reference = reference;
		}

		// Compares the glob pattern against
        // the file or directory name.
        void find(Path file) {
            Path name = file.getFileName();
            if (name != null && matcher.matches(name)) {
                numMatches++;
                Base<String> base = new AbstractBase<String>();
                base.setId(file.toString());
                reference.add(base);
                System.out.println("matches " + numMatches);
            }
        }

        // Prints the total number of
        // matches to standard out.
        void done() {
            System.out.println("Matched: "
                + numMatches);
        }

        // Invoke the pattern matching
        // method on each file.
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        // Invoke the pattern matching
        // method on each directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                BasicFileAttributes attrs) {
            find(dir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file,
                IOException exc) {
            System.err.println(exc);
            return CONTINUE;
        }
    }

	public Queue<Base<?>> getFileNameQueue() {
		return fileNameQueue;
	}

	public void setFileNameQueue(Queue<Base<?>> fileNameQueue) {
		this.fileNameQueue = fileNameQueue;
	}
}
